<template>
	<view class="zSearchWraper">
		<view class="top">
			<image @click="backto" src="../../static/images/iconImg/zjt.png" mode=""></image>
			<text>搜索</text>
		</view>

		<view class="zSearchOuter">
			<view class="zSearch">
				<image src="../../static/images/iconImg/sousuo.png" mode=""></image>
				<input type="search" v-model="keyWord" placeholder="请输入关键词搜索">
				<image v-if="keyWord!==''" @click="clearKeyWord" src="../../static/images/iconImg/chahao.png" mode="">
				</image>
			</view>
			<button class="searchBtn" @click="search">搜索</button>
		</view>

		<view class="searchMain">

			<view class="history" v-if="historyTab.length !== 0">
				<view class="title">
					<text>历史记录</text>
					<text @click="clearTab">清除</text>
				</view>
				<ul class="historyItem">
					<li v-for="item in historyTab" :key="item.id" @click="historyBtn(item.title)">
						{{item.title}}
					</li>
				</ul>
			</view>

			<view class="discoverOuter">
				<text>搜索发现</text>
				<ul class="discover">
					<li v-for="item in discoverData" @click="seachFind(item.title)">
						{{item.title}}
					</li>
				</ul>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				discoverData: [{
					id: '1',
					title: '羽绒服'
				}, {
					id: '2',
					title: 'iphone'
				}, {
					id: '3',
					title: '小米mix'
				}, ],

				historyTab: [],
				keyWord: '',
			};
		},
		onLoad(options) {
			this.keyWord = options.keyWord
		},
		created() {
			const history = uni.getStorageSync('historyTab')
			this.historyTab = history
		},
		methods: {
			// 返回
			backto() {
				uni.switchTab({
					url: '/pages/home/home'
				})
			},

			// 检查重复元素
			checkDuplication(guanjianzi, beijiancha) {
				let flag = false
				for (let i = 0; i < beijiancha.length; i++) {
					if (beijiancha[i].title === guanjianzi) {
						flag = true
					}
				}
				return flag

			},

			// 清空按钮
			clearKeyWord() {
				this.keyWord = ''
			},

			// 搜索按钮
			search() {
				if (this.keyWord === '') {
					uni.showToast({
						title: '请输入关键字!',
						icon: 'none',
						duration: 1000
					});
				} else if (this.historyTab.length === 0) {
					const arr = []
					const obj = {
						id: this.historyTab.length + 1,
						title: this.keyWord
					}
					arr.push(obj)
					this.historyTab = arr
					uni.setStorageSync('historyTab', this.historyTab)
					uni.navigateTo({
						url: '/pages/searchItem/searchItem?keyWord=' + this.keyWord
					})
				} else {
					const res = this.checkDuplication(this.keyWord, this.historyTab)
					console.log(res);

					if (res) {
						uni.navigateTo({
							url: '/pages/searchItem/searchItem?keyWord=' + this.keyWord
						})
					} else {
						const obj = {
							id: this.historyTab.length + 1,
							title: this.keyWord
						}
						this.historyTab.unshift(obj)
						uni.setStorageSync('historyTab', this.historyTab)
						uni.navigateTo({
							url: '/pages/searchItem/searchItem?keyWord=' + this.keyWord
						})
					}
					console.log(res);
				}
			},

			// 清空历史记录
			clearTab() {
				uni.removeStorageSync('historyTab')
				this.historyTab = []
				this.keyWord = ''
			},

			// 历史记录点击
			historyBtn(data) {
				this.keyWord = data
				uni.navigateTo({
					url: '/pages/searchItem/searchItem?keyWord=' + this.keyWord
				})
			},

			// 搜索发现点击
			seachFind(data) {
				this.keyWord = data
				if (this.historyTab.length === 0) {
					const arr = []
					arr.unshift({
						id: this.historyTab.length + 1,
						title: data
					})
					this.historyTab = arr
					uni.setStorageSync('historyTab', arr)
					uni.navigateTo({
						url: '/pages/searchItem/searchItem?keyWord=' + this.keyWord
					})
				} else {
					// 检查重复元素
					const res = this.checkDuplication(data, this.historyTab)
					if (res) {
						uni.navigateTo({
							url: '/pages/searchItem/searchItem?keyWord=' + this.keyWord
						})
					} else {
						this.historyTab.unshift({
							id: this.historyTab.length + 1,
							title: data
						})
						uni.setStorageSync('historyTab', this.historyTab)
						uni.navigateTo({
							url: '/pages/searchItem/searchItem?keyWord=' + this.keyWord
						})
					}
				}

			},

		}
	}
</script>

<style lang="scss" scoped>
	.top {
		height: 36px;
		text-align: center;
		padding-top: 10px;
		font-size: 16px;
		font-weight: bold;
		display: flex;
		align-items: center;

		image {
			width: 24px;
			height: 20px;
			margin-right: 40%;
		}
	}

	.zSearchWraper {
		height: 93.4vh;
		width: 100vw;
		overflow: hidden;
	}

	.zSearchOuter {
		width: 100%;
		margin-top: 10px;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.zSearch {
			width: 70%;
			height: 34px;
			background-color: #ececec;
			border-radius: 100px;
			padding: 10px 0 10px 10px;
			display: flex;
			align-items: center;
			margin-left: 10px;

			image {
				width: 20px;
				height: 20px;
				margin-right: 10px;
			}

			input {
				height: 100%;
				margin-right: 10px;
			}
		}

		.searchBtn {
			height: 34px;
			line-height: 34px;
			font-size: 14px;
		}
	}

	.searchMain {
		width: 100%;
		height: 85vh;
		background-color: #f5f5f5;
		margin-top: 10px;
		padding: 10px;
		overflow-y: scroll;

		.history {
			width: 100%;
			padding-bottom: 40px;

			.title {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 10px;
			}

			.historyItem {
				display: flex;
				flex: 1;
				flex-wrap: wrap;

				li {
					padding: 6px;
					background-color: #fff;
					margin-right: 10px;
					font-size: 14px;
					margin-bottom: 10px;
				}
			}
		}

		.discoverOuter {
			.discover {
				margin-top: 10px;
				padding: 0;
				display: flex;
				align-items: center;

				li {
					padding: 5px 10px;
					font-size: 12px;
					background-color: #fff;

					&:not(last-child) {
						margin-right: 10px;
					}
				}
			}
		}

	}
</style>
